package com.baibei.order.trend;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baibei.model.ProductInfo;
import com.baibei.module.stock.simple.SimpleStockFragment;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.blankj.utilcode.utils.ScreenUtils;
import com.rae.widget.dialog.impl.SlideDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 价格走势弹窗碎片
 * Created by shenminjie on 2017/8/14.
 * mail:shenminjie@100bei.com
 */

public class PriceTrendFragment extends DialogFragment {

    @BindView(R2.id.layout_container)
    LinearLayout mLayoutContainer;
    Unbinder unbinder;

    private String mProductId;

    public static final String PARAM = "PARAM_ID";

    /**
     * 获取实例
     *
     * @param id id
     * @ return return
     */
    public static PriceTrendFragment newInstance(String id) {
        PriceTrendFragment fragment = new PriceTrendFragment();
        Bundle args = new Bundle();
        args.putString(PARAM, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mProductId = bundle.getString(PARAM);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_price_trend, container, false);
        unbinder = ButterKnife.bind(this, view);
        ViewGroup.LayoutParams params = mLayoutContainer.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth();
        params.height = (ScreenUtils.getScreenHeight() / 2);
        mLayoutContainer.setLayoutParams(params);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ProductInfo info = new ProductInfo();
        info.setProductId(mProductId);
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_container, SimpleStockFragment.newInstance(info.getProductId())).commitAllowingStateLoss();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new SlideDialog(getContext());
    }

    @Override
    public void onDestroyView() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        super.onDestroyView();
    }

    @OnClick(R2.id.tv_close)
    public void onClose() {
        dismiss();
    }
}
